import "package:flutter/material.dart";
import 'package:flutter_pink_app/serialized/recommend.dart';
import 'package:flutter_pink_app/style/decoration.dart';
import 'package:flutter_pink_app/style/text.dart';
import 'package:flutter_pink_app/util.dart';
import "../route/delegate.dart";
import '../route/page.dart';
import 'icon_text_match.dart';

// 视频单列组合
class VideoRow extends StatelessWidget {
  final VideoInfo _videoInfo; // 视频信息
  final double rowHeight; // 展示图片高度
  final EdgeInsetsGeometry padding; // 内边距
  final double imgCircularBorder; // 图片圆角边框

  const VideoRow(this._videoInfo,
      {Key key,
      this.rowHeight = 90,
      this.padding = const EdgeInsets.all(10),
      this.imgCircularBorder = 5})
      : super(key: key);

  // 打开新路由页方法
  void _openNewRoute(BuildContext ctx) {
    final delegate = BiliRouterDelegate.of(ctx); // 路由代理
    final peek = delegate.peek();
    try {
      // 如果当前是影像页，则执行replace操作，否则执行push操作
      if (peek.path == "/video") {
        delegate.replace(BiliPageStatus("/video", videoInfo: _videoInfo));
      } else {
        delegate.push(BiliPageStatus("/video", videoInfo: _videoInfo));
      }
    } catch (e) {
      logCurrStackTrace();
      print("试图打开新的视频页面的过程中失败,$e");
    }
  }

  // 构建左侧图片栏方法
  ClipRRect _buildLeftImg() {
    return ClipRRect(
      borderRadius: BorderRadius.circular(imgCircularBorder),
      child: Stack(
        children: <Widget>[
          Image.network(_videoInfo.cover,
              fit: BoxFit.cover, height: rowHeight, width: rowHeight * 16 / 9),
          Positioned(
              bottom: 10,
              right: 5,
              child: Container(
                decoration: blackBubbleBackground,
                padding: const EdgeInsets.all(3),
                child: Text(
                  formatVideoTime(_videoInfo.duration),
                  style: tinyWhiteStyle,
                ),
              ))
        ],
      ),
    );
  }

  // 构建右侧介绍方法
  Container _buildRightIntro() {
    return Container(
      height: rowHeight,
      padding: const EdgeInsets.only(left: 8.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween, // 两端对齐
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Text(_videoInfo.title,
              style: videoCardStyle,
              maxLines: 3,
              overflow: TextOverflow.ellipsis), // 兼容小屏幕手机，最多三行
          _buildRightContent(),
        ],
      ),
    );
  }

  // 构建右侧内容
  Column _buildRightContent() {
    final owner = _videoInfo.owner; // 取得作者信息
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        Text('UP主： ${owner?.name}', style: ownerCardStyle),
        _buildRightBottom(),
      ],
    );
  }

  // 构建右侧底层内容
  Padding _buildRightBottom() {
    return Padding(
      padding: const EdgeInsets.only(top: 3),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween, // 两端对齐
        children: <Widget>[
          Row(
            children: [
              IconTextMatch(
                  text: formatPlayCount(_videoInfo.view),
                  icon: Icons.ondemand_video_rounded),
              IconTextMatch(
                  text: formatVideoCount(_videoInfo.reply),
                  icon: Icons.list_alt)
            ],
          ),
          IconTextMatch(icon: Icons.more_vert)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext videoRowCtx) {
    return GestureDetector(
      onTap: () => _openNewRoute(videoRowCtx),
      child: Container(
        decoration: bottomLineBoxDecoration,
        padding: padding,
        child: Row(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            _buildLeftImg(),
            Expanded(child: _buildRightIntro())
          ],
        ),
      ),
    );
  }
}
